# __getitem__(): 实现对象的索引访问
class MyList:
    def __init__(self,data):
        self.data = data

    def __getitem__(self,index):
        print("__getitem__()被调用")
        return self.data[index]

mylist = MyList([1,2,3,4,5])
print(mylist[0])

print("=" * 50)
# __setitem__(): 实现对象的赋值操作
class MyList2:
    def __init__(self,data):
        self.data = data

    def __setitem__(self, key, value):
        print("__setitem__()被调用")
        self.data[key] = value

mylist2 = MyList2([1,2,3,4,5])
mylist2[0] = 100
print(mylist2.data)

print("=" * 50)
# __delitem__(): 实现对象按特定索引删除
class MyList3:
    def __init__(self,data):
        self.data = data

    def __delitem__(self, key):
        print("__delitem__()被调用")
        del self.data[key]

mylist3 = MyList3([1,2,3,4,5])
del mylist3[0]
print(mylist3.data)

print("=" * 50)
# __len__(): 实现对象长度的获取
class MyList4:
    def __init__(self,data):
        self.data = data

    def __len__(self):
        print("__len__()被调用")
        return len(self.data)

mylist4 = MyList4([1,2,3,4,5])
print(len(mylist4))

print("=" * 50)
# __contains__(): 实现对象成员判断
class MyList5:
    def __init__(self,data):
        self.data = data

    def __contains__(self, item):
        print("__contains__()被调用")
        return item in self.data

mylist5 = MyList5([1,2,3,4,5])
print(1 in mylist5)